<template>
  <div>
    <div class="node" v-has="{action: 'subsidy:order:list', effect: 'move'}">
    <div :class="[$style.search_box, $style.topSelect]">
      <SearchInput
        placeholder="请输入"
        label="订单编号"
        style="margin-right:20px"
        :value="searchs.oid"
        :onChange="(val) => changeSearch('oid',val)"
      />
      <div :class="$style.selectBox">
        <label>状态：</label>
        <el-select
          style="width:160px"
          v-model="searchs.state"
          clearable
          placeholder="请选择"
        >
          <el-option label="推送成功" :value="1"></el-option>
          <el-option label="推送失败" :value="2"></el-option>
          <el-option label="等待推送" :value="0"></el-option>
        </el-select>
      </div>
      <div :class="$style.selectBox" style="margin-right:20px;display: flex;align-items: center;">
        <label>创建时间：</label>
        <el-date-picker
          v-model="searchs.appointmentTime"
          type="daterange"
          unlink-panels
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          value-format="timestamp"
          :picker-options="pickerOptions"
        ></el-date-picker>
      </div>
      <div :class="$style.selectBox">
        <el-button size="small" plain  icon="el-icon-search" @click="getQuerydata">查询</el-button>        
      </div>
    </div>
    <Panel title="市政补贴订单推送">
      
    <TableEncapsulation
      slot="body"
      :tableConfig="tableConfig"
      :tableData="tableData"
    >
    <template slot="operation" slot-scope="operation">
          <el-button
            type="text"
            @click="()=>editStore(operation.row.id)"
          >编辑</el-button>
          <el-button
            type="text"
            @click="()=>remove(operation.row.id)"
          >删除</el-button>
        </template>
        <TablePager
          slot="pager"
          v-if="pager.use"
          :size="pager.size"
          :pageChange="pageChange"
          :show-total="pager.showTotal"
          :total="pager.total"
          :current="pager.current"
        >
          <span class="demonstration">GO></span>
        </TablePager>
    </TableEncapsulation>
    </Panel>
    </div>
    <div style="display: none;width:100%;height:100%;margin: auto;" v-has="{action: 'subsidy:order:list', effect: 'noPremiss'}">
      <NoPremission></NoPremission>
    </div>
  </div>
</template>
<script>
import request from "@/tools/request";
import Panel from "@/components/Panel";
import TableEncapsulation from "@/components/TableEncapsulation";
import TablePager from "@/components/Pager";
import SearchInput from "@/components/SearchInput";
import { objectFilter } from "@/tools/array.js"
import { dateFormat } from "@/tools/time";
export default {
  name: "orderPush",
  components: {
    TableEncapsulation,
    TablePager,
    Panel,
    SearchInput
  },
  data() {
    return {
      tableData: [],
      searchs:{
        component: 'push',
        oid: "",
        state: '',
        appointmentTime: [new Date().getTime() - 3600 * 1000 * 24 * 1, new Date().getTime()],
      },
       pager: {
        component: 'push',
        use: true,
        total: 0,
        current: 1,
        size: 10,
        showTotal: true
      },
      operation: true,
      pickerOptions: {
          shortcuts: [{
            text: '最近一周',
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit('pick', [start, end]);
            }
          }, {
            text: '最近一个月',
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit('pick', [start, end]);
            }
          }, {
            text: '最近三个月',
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
              picker.$emit('pick', [start, end]);
            }
          }]
        },
    };
  },
  created() {
    this.searchsFnc()
    this.getQuerydata();
  },
  computed: {
    tableConfig () {
      return {
        selection: true,
        columnConfig: [
          {
            label: "订单编号",
            type: "text",
            prop: "oid",
            align: "center"
          },
          {
            label: "状态",
            type: "other",
            prop: "state",
            align: "center",
            fnc: val => val==1? '推送成功' : val == 2 ? '推送失败': '等待推送',
            class:  val => val == 1 ? "blue-tag" : val == 2 ? "cancel" : "wait",
          },
          {
            label: "推送时间",
            type: "text",
            prop: "notifyTime",
            align: "center"
          },
          {
            label: "重复次数",
            type: "text",
            prop: "retryNum",
            align: "center"
          },
          {
            label: "下次推送时间",
            type: "text",
            prop: "nextTime",
            align: "center"
          },
          {
            label: "错误消息",
            type: "text",
            prop: "msg",
            align: "center"
          },
        //   this.operation && {
        //     type: "operation",
        //     align: "center",
        //     label: "操作",
        //     width:180
        //   }
        ].filter(Boolean)
        }
    }
  },
  methods: {
    searchsFnc (){
      let searchs = this.$store.getters.searchs
      if(searchs &&searchs.component == 'push'){
        this.searchs = searchs
      }
      let pager = this.$store.getters.pager
      if(pager &&pager.component == 'push'){
        this.pager = pager
      }
    },
    async getQuerydata() {
      let endTime = this.searchs.appointmentTime ? (dateFormat(this.searchs.appointmentTime[1] + 86400000-1, "yyyy-MM-dd HH:mm:ss")) : undefined
      let beginTime = this.searchs.appointmentTime ? (dateFormat(this.searchs.appointmentTime[0],"yyyy-MM-dd HH:mm:ss")) : undefined
      let page = this.pager.current, state= this.searchs.state, oid = this.searchs.oid
      let search = objectFilter({ page, state, oid, endTime, beginTime})
      const res = await request({
        url: "/subsidy/list",
        method: "POST",
        data: search
      });
      this.tableData = res.page.list
      this.pager.total = res.page.totalCount
      this.pager.current = res.page.currPage
    },
    pageChange(val) {
      this.pager.current = val;
      this.getQuerydata();
    },
    changeSearch(key, val) {
      this.searchs[key] = val;
      this.query.page = 1;
    },
    editStore(id){
      this.$store.commit('saveSearchs', this.searchs)
      this.$store.commit('savePager', this.pager)
      this.$router.push({ path: "/wx/advise/info",query:{id:id} })
    },
    commentDetail(id){
      this.$router.push({ path: "/storeDetail",query:{id:id,type: "toview"} })
    },
    remove(id){
       this.$confirm('确认删除吗', '操作确认', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then( async () => {
        const del = await request({
        url: "/wx/index/activity/delete",
        dataType:'json',
        method: "POST",
        data:[id]
      });
        if(del.code !==0) return
        this.$message.success('删除成功')
        if(this.tableData.length===1 && this.pager.current>1)
          this.$set(this.pager,'current', this.pager.current-1)
        this.getQuerydata()
      })
    },
  }
}
</script>
<style lang='scss' module>
.examine{
    display: flex;
    align-items: center;
    :global{
        div{
            color: #fff;
            padding: 10px;
            margin: 0 10px;
            border-radius: 2px;
            font-size: 14px;
            cursor: pointer;
            min-width: 60px;
            text-align: center;
        }
        .adopt{
                background: #409EFF;
        }
    }
}
img {
    width: 88px;
    border-radius: 5px;
}
.search_box {
  display: flex;
  align-items: center;
  flex-wrap: wrap;

  :global {
    .el-input {
      width: 150px;
    }
    .el-select {
      .el-input {
        width: 90px;
      }
    }
    .el-input__icon {
      line-height: 27px;
    }
    .el-range-separator{
      line-height: 26px;
    }
  }
}

.topSelect {
  background-color: white;
  border-radius: 4px;
  padding: 20px;
  margin-bottom: 20px;
}
</style>